home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / cicnAnimDemo 1.0.2 / cicnAnim.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  7.3 KB  |  403 lines  |  [TEXT/CWIE]

  1. // cicnAnim
  2. // by Steve Chamberlin
  3. // ported to CodeWarrior by Ken Long
  4. // converted to CW7 on 951201
  5.  
  6. #include "QDOffscreen.h"
  7.  
  8. MenuHandle    appleMenu, fileMenu, editMenu, extraMenu;
  9.  
  10. #define appleID        1
  11. #define fileID        256
  12. #define editID        257
  13. #define extraID        258
  14.  
  15. #define openItem    1
  16. #define    closeItem    2
  17. #define quitItem    4
  18.  
  19. #define WIDTH        200
  20. #define HEIGHT        150
  21. #define ITEMWIDTH    32
  22. #define ITEMHEIGHT    32
  23.  
  24. Rect        bRect, items [20], bbox;
  25. short        x [20], y [20], vx [20], vy [20], color [20], max = 5;
  26. GWorldPtr    offscreen;
  27. CIconHandle            cicn [12];
  28. PixPatHandle        myPat;
  29. Boolean                Is32Bit ();
  30.  
  31. WindowPtr        myWindow;
  32. CWindowRecord     wRecord;
  33. Rect            dragRect;
  34.  
  35. void SetUpWindow (void);
  36. void DrawMy (Boolean hilited);
  37. void SetUpMenus (void);
  38. void AdjustMenus (void);
  39. void MyEnable (MenuHandle menu, short item, Boolean ok);
  40. void HandleMenu (long mSelect);
  41. Boolean Is32Bit (void);
  42. void InitMacintosh (void);
  43. void HandleMouseDown (EventRecord * theEvent);
  44. void HandleEvent (void);
  45. short RandomInt (short range);
  46. void SetUpItems (void);
  47. void LoadSwill (void);
  48. void SetUpOffscreen (void);
  49. void DrawFrame (void);
  50. void Animate (void);
  51. void main (void);
  52.  
  53. void SetUpWindow ()
  54. {
  55.     dragRect = qd.screenBits.bounds;
  56.  
  57.     myWindow = GetNewCWindow (256, &wRecord, (WindowPtr) - 1L);
  58.     SetPort (myWindow);
  59. }
  60.  
  61. void DrawMy (Boolean hilited)
  62. {
  63.     //• put redraw window code here, if needed
  64. }
  65.  
  66. void SetUpMenus ()
  67. {
  68.     appleMenu = GetMenu (appleID);
  69.     fileMenu = GetMenu (fileID);
  70.     editMenu = GetMenu (editID);
  71.     extraMenu = GetMenu (extraID);
  72.     AddResMenu (appleMenu, 'DRVR');
  73.     InsertMenu (appleMenu, 0);
  74.     InsertMenu (fileMenu, 0);
  75.     InsertMenu (editMenu, 0);
  76.     InsertMenu (extraMenu, 0);
  77.     CheckItem (extraMenu, 1, true);
  78.     DrawMenuBar ();
  79. }
  80.  
  81. void AdjustMenus ()
  82. {
  83.     WindowPeek wp;
  84.     short kind;
  85.     Boolean DA;
  86.  
  87.     wp = (WindowPeek) FrontWindow ();
  88.     kind = wp ? wp->windowKind : 0;
  89.     DA = kind < 0;
  90.  
  91.     MyEnable (editMenu, 1, DA);
  92.     MyEnable (editMenu, 3, DA);
  93.     MyEnable (editMenu, 4, DA);
  94.     MyEnable (editMenu, 5, DA);
  95.     MyEnable (editMenu, 6, DA);
  96.  
  97.     MyEnable (fileMenu, openItem, ! ((WindowPeek) myWindow)->visible);
  98.     MyEnable (fileMenu, closeItem, DA || ((WindowPeek) myWindow)->visible);
  99. }
  100.  
  101. void MyEnable (MenuHandle menu, short item, Boolean ok)
  102. {
  103.     if (ok)
  104.         EnableItem (menu, item);
  105.     else
  106.         DisableItem (menu, item);
  107. }
  108.  
  109. void HandleMenu (long mSelect)
  110. {
  111.     short            i, menuID = HiWord (mSelect);
  112.     short            menuItem = LoWord (mSelect);
  113.     Str255        name;
  114.     GrafPtr        savePort;
  115.     WindowPeek    frontWindow;
  116.  
  117.     switch (menuID)
  118.     {
  119.         case    appleID:
  120.             if (menuItem == 1)
  121.             {
  122.                 Alert (9999, 0L);
  123.                 break;
  124.             }
  125.             else
  126.                 {
  127.                     GetPort (&savePort);
  128.                     GetItem (appleMenu, menuItem, name);
  129.                     OpenDeskAcc (name);
  130.                     SetPort (savePort);
  131.             }
  132.         break;
  133.     
  134.         case    fileID:
  135.             switch (menuItem)
  136.             {
  137.                 case    openItem:
  138.                     ShowWindow (myWindow);
  139.                     SelectWindow (myWindow);
  140.                 break;
  141.     
  142.                 case    closeItem:
  143.                     if ((frontWindow = (WindowPeek) FrontWindow ()) == 0L)
  144.                     break;
  145.                     if (frontWindow->windowKind < 0)
  146.                         CloseDeskAcc (frontWindow->windowKind);
  147.                     else if (frontWindow = (WindowPeek) myWindow)
  148.                             HideWindow (myWindow);
  149.                  break;
  150.      
  151.                  case    quitItem:
  152.                     ExitToShell ();
  153.                 break;
  154.             }
  155.         break;
  156.     
  157.         case    editID:
  158.             if (! SystemEdit (menuItem - 1))
  159.                 SysBeep (5);
  160.         break;
  161.  
  162.         case    extraID:
  163.             if (menuItem <= 4)
  164.             {
  165.                 for (i = 1; i < 5; i++)
  166.                     CheckItem (extraMenu, i, false);
  167.         
  168.                 max = menuItem * 5;
  169.                 CheckItem (extraMenu, menuItem, true);
  170.             }
  171.             if (menuItem == 6)
  172.                 SetUpItems ();
  173.                 break;
  174.     }
  175. }
  176.  
  177. Boolean Is32Bit ()
  178. {
  179.     SysEnvRec mySE;
  180.  
  181.     SysEnvirons (2, &mySE);
  182.  
  183.     if (! mySE.hasColorQD)
  184.         return (false);
  185.  
  186.     return (NGetTrapAddress (0xAB03, ToolTrap) !=
  187.             NGetTrapAddress (0xA89F, ToolTrap));
  188. }
  189.  
  190. void InitMacintosh ()
  191. {
  192.     MaxApplZone ();
  193.  
  194.     InitGraf (&qd.thePort);
  195.     InitFonts ();
  196.     FlushEvents (everyEvent, 0);
  197.     InitWindows ();
  198.     InitMenus ();
  199.     TEInit ();
  200.     InitDialogs (0L);
  201.     InitCursor ();
  202.  
  203. }
  204.  
  205. void HandleMouseDown (EventRecord * theEvent)
  206. {
  207.     WindowPtr    theWindow;
  208.     short            windowCode;
  209.  
  210.     windowCode = FindWindow (theEvent->where, &theWindow);
  211.     switch (windowCode)
  212.     {
  213.             case inSysWindow:
  214.                 SystemClick (theEvent, theWindow);
  215.             break;
  216.         
  217.             case inMenuBar:
  218.                 AdjustMenus ();
  219.                 HandleMenu (MenuSelect (theEvent->where));
  220.             break;
  221.         
  222.             case inDrag:
  223.                 if (theWindow == myWindow)
  224.                         DragWindow (myWindow, theEvent->where, &dragRect);
  225.             break;     
  226.         
  227.             case inContent:
  228.                 if (theWindow == myWindow)
  229.                 {
  230.                     if (theWindow != FrontWindow ())
  231.                         SelectWindow (myWindow);
  232.                     else
  233.                         InvalRect (&myWindow->portRect);
  234.                 }
  235.             break;
  236.  
  237.             case inGoAway:
  238.                 if (theWindow == myWindow &&
  239.                     TrackGoAway (myWindow, theEvent->where))
  240.                 HideWindow (myWindow);
  241.             break;
  242.     }
  243. }
  244.  
  245. void HandleEvent ()
  246. {
  247.     EventRecord    theEvent;
  248.  
  249.     HiliteMenu (0);
  250.     WaitNextEvent (everyEvent, &theEvent, 0L, 0L);
  251.     switch (theEvent.what)
  252.     {
  253.         case mouseDown:
  254.             HandleMouseDown (&theEvent);
  255.             break;
  256.         case keyDown:
  257.         case autoKey:
  258.             if ((theEvent.modifiers & cmdKey) != 0)
  259.             {
  260.                 AdjustMenus ();
  261.                 HandleMenu (MenuKey ((char) (theEvent.message & charCodeMask)));
  262.             }
  263.             break;
  264.         case updateEvt:
  265.             BeginUpdate (myWindow);
  266.             DrawMy (((WindowPeek) myWindow)->hilited);
  267.             EndUpdate (myWindow);
  268.             break;
  269.         case activateEvt:
  270.             InvalRect (&myWindow->portRect);
  271.             break;
  272.         }
  273. }
  274.  
  275. short RandomInt (short range)
  276. {
  277.     long rawResult;
  278.  
  279.     rawResult = Random ();
  280.  
  281.     if (rawResult < 0)
  282.         rawResult *= - 1;
  283.     
  284.     return ((rawResult * range) / 32768);
  285. }
  286.  
  287. void SetUpItems ()
  288. {
  289.     short i, n;
  290.  
  291.     for (i = 0; i < 20; i++)
  292.     {
  293.         x [i] = RandomInt (WIDTH - 2 * ITEMWIDTH) + ITEMWIDTH;
  294.         y [i] = RandomInt (HEIGHT - 2 * ITEMHEIGHT) + ITEMHEIGHT;
  295.         while ((n = RandomInt (13) - 6) == 0);
  296.         vx [i] = n;
  297.         while ((n = RandomInt (13) - 6) == 0);
  298.         vy [i] = n;
  299.         color [i] = RandomInt (12);
  300.         SetRect (&items [i], x [i], y [i], x [i] + ITEMWIDTH, y [i] + ITEMHEIGHT);
  301.     }
  302. }
  303.  
  304. void LoadSwill ()
  305. {
  306.     short i;
  307.  
  308.     for (i = 0; i < 12; i++)
  309.         cicn [i] = GetCIcon (256 + i);
  310.  
  311.     myPat = GetPixPat (256);
  312. }
  313.  
  314. void SetUpOffscreen ()
  315. {
  316.     GDHandle    saveGD;
  317.     GWorldPtr    saveGW;
  318.     QDErr        errcode;
  319.  
  320.     SetRect (&bRect, 0, 0, WIDTH, HEIGHT);
  321.  
  322.     GetGWorld (&saveGW, &saveGD);
  323.     errcode = NewGWorld (&offscreen, 8, &bRect, 0L, 0L, 0L);
  324.     if (errcode != noErr)        //• could not create new GWorld
  325.     {
  326.         SysBeep (1);
  327.         ExitToShell ();
  328.     }
  329.     LockPixels (offscreen->portPixMap);
  330.     SetGWorld (offscreen, 0L);
  331.     EraseRect (&bRect);
  332.     SetGWorld (saveGW, saveGD);
  333.     UnlockPixels (offscreen->portPixMap);
  334. }
  335.  
  336. void DrawFrame ()
  337. {
  338.     GDHandle     saveGD;
  339.     GWorldPtr    saveGW;
  340.  
  341.     GetGWorld (&saveGW, &saveGD);
  342.     LockPixels (offscreen->portPixMap);
  343.     SetGWorld (offscreen, 0L);
  344.     
  345.     FillCRect (&bRect, myPat);
  346.     Animate ();
  347.     
  348.     SetGWorld (saveGW, saveGD);
  349.     SetPort (myWindow);
  350.     
  351.     CopyBits ((BitMap*)*offscreen->portPixMap, 
  352.                &myWindow->portBits, 
  353.                &bRect,
  354.                &bRect, srcCopy, 0L);
  355.             
  356.     UnlockPixels (offscreen->portPixMap);
  357. }
  358.  
  359. void Animate ()
  360. {
  361.     short i;
  362.  
  363.     for (i = 0; i < max; i++)
  364.     {
  365.         PlotCIcon (&items [i], cicn [color [i]]);
  366.  
  367.         if (x [i] > WIDTH - ITEMWIDTH || x [i] < 0)
  368.             vx [i] *= - 1;
  369.         if (y [i] > HEIGHT - ITEMHEIGHT || y [i] < 0)
  370.             vy [i] *= - 1;
  371.         OffsetRect (&items [i], vx [i], vy [i]);
  372.         x [i] = items [i].left;
  373.         y [i] = items [i].top;
  374.     }
  375. }
  376.  
  377. void main ()
  378. {
  379.     short i;
  380.  
  381.     InitMacintosh ();
  382.     if (! Is32Bit ())     //• must have 32 bit Quickdraw
  383.     {
  384.         StopAlert (400, 0L);
  385.         ExitToShell ();
  386.     }
  387.     SetUpMenus ();
  388.     SetUpWindow ();
  389.  
  390.     GetDateTime ((unsigned long*) qd.randSeed);
  391.  
  392.     SetUpOffscreen ();
  393.     SetUpItems ();
  394.     LoadSwill ();
  395.  
  396.     for (; ; )
  397.     {
  398.         HandleEvent ();
  399.         DrawFrame ();
  400.     }
  401. }
  402.  
  403.